/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package androidpos;

import androidpos.network.Protocol;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author user
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static int port = 5432;
    public static Socket socket;
    public static DataOutputStream dos;
    public static DataInputStream dis;

    public static boolean androidFirstSync() throws Exception {
        ServerSocket ssocket = new ServerSocket(port);
        System.out.println("===== POS Machine is running ====");
        System.out.println("Waiting for the connection...");
        socket = ssocket.accept();
        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());
        String msg = dis.readUTF();
        Map<String, String> dataMap = parseCommand(msg);
        if (dataMap.containsKey(Protocol.MSG_JOIN) && dataMap.get(Protocol.MSG_JOIN).equals(Protocol.DATA_OK)) {
            System.out.println("Some one connected...");
            System.out.println("IP : " + socket.getInetAddress().getHostAddress());

            //Send ACKNOWLEDGE message
            String ackCommand = genCommand(Protocol.MSG_ACK, Protocol.DATA_OK);
            dos.writeUTF(ackCommand);
            System.out.println("Send acknowledge command ==> " + ackCommand);

            //Send the amouth
            double amount = 123456.78;
            String unit = "THB";
            String paymentDetail = genCommand(Protocol.PAY_AMOUNT, amount + "") + genCommand(Protocol.PAY_UNIT, unit);

            dos.writeUTF(paymentDetail);
            System.out.println("SEND THE AMOUNT OF MONEY ==> " + paymentDetail);


            return true;
        }
        dos.writeUTF(genCommand(Protocol.MSG_ACK, Protocol.DATA_FAIL));
        return false;

    }

    public static boolean androidSecondSync() {
        try {
            String paymentMsg = dis.readUTF();
            Map<String, String> paymentData = parseCommand(paymentMsg);
            if (!paymentData.containsKey(Protocol.CARD_NUM) || !paymentData.containsKey(Protocol.CARD_TYPE) || !paymentData.containsKey(Protocol.CARD_SECCODE) || !paymentData.containsKey(Protocol.CARD_EXP_MONTH) || !paymentData.containsKey(Protocol.CARD_EXP_YEAR) || !paymentData.containsKey(Protocol.CARD_NAME)) {
                System.out.println("INVALID COMMAND => " + paymentMsg);
                return false;
            }
            System.out.println("ANDROID PAY MONEY ==> " + paymentMsg);

            if (isAccountVaild()) {
                dos.writeUTF(genCommand(Protocol.MSG_ACK, Protocol.DATA_OK));
                System.out.println("PAY SUCCESS");
                return true;
            }
            dos.writeUTF(genCommand(Protocol.MSG_ACK, Protocol.DATA_FAIL));
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isAccountVaild() {
        return true;
    }

    public static void main(String[] args) throws Exception {
        // Hello Narin , Hello Mao
        if (androidFirstSync() && androidSecondSync()) {
            System.out.println("===== SUCCESS ===");

        } else {
            System.out.println("===== FAIL =====");
        }
    }

    private static Map<String, String> parseCommand(String txt) {
        Map<String, String> dataMap = new HashMap<String, String>();
        String datas[] = txt.split(";");
        for (String data : datas) {
            String header = data.split(":")[0].trim();
            String msg = data.split(":")[1].trim();
            dataMap.put(header, msg);
        }
        return dataMap;
    }

    private static String genCommand(String key, String value) {
        return key + ":" + value + ";";
    }
}
